package com.blockmeta.bbs.immodule.activity;

import android.graphics.Bitmap;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blockmeta.bbs.baselibrary.provider.IApplicationProvider;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.umeng.MEDIA_TYPE;
import com.blockmeta.bbs.businesslibrary.umeng.ScreenShotUtil;
import com.blockmeta.bbs.businesslibrary.umeng.ShareResTask;
import com.blockmeta.bbs.businesslibrary.umeng.SharedUtils;
import com.blockmeta.bbs.businesslibrary.umeng.TypeDescribe;
import com.blockmeta.bbs.businesslibrary.umeng.circle.IShareObject;
import com.blockmeta.bbs.businesslibrary.umeng.circle.ShareContent;
import com.blockmeta.bbs.businesslibrary.util.AppCode;
import com.blockmeta.bbs.businesslibrary.util.CodeUtils;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.bean.Params;

import butterknife.BindView;
import butterknife.OnClick;

public class PersonalQrCodeActivity extends BaseActivity {

    @BindView(R2.id.iv_circle_icon)
    CircleImageView mIvCircleIcon;
    @BindView(R2.id.tv_circle_name)
    TextView mTvCircleName;
    @BindView(R2.id.tv_circle_info)
    TextView mTvCircleInfo;
    @BindView(R2.id.iv_qr)
    ImageView mIvQr;
    @BindView(R2.id.cl_share_container)
    ConstraintLayout mShareArea;

    private long mFriendID;

    private String mName,mAvatar,mDesc;

    @Override
    protected boolean showTitle() {
        return false;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_personal_qr_code, null);
    }

    @Override
    protected void initialize() {
        super.initialize();

        mFriendID = getIntent().getLongExtra(Params.USERID,0);
        mName = getIntent().getStringExtra(Params.USER_NAME);
        mAvatar = getIntent().getStringExtra(Params.USER_AVATAR);
        mDesc = getIntent().getStringExtra(Params.USER_DESC);

        LoadPicUtil.loadWithPicasso(mIvCircleIcon, mAvatar);
        mTvCircleName.setText(mName);
        mTvCircleInfo.setText(mDesc);

        int wh = DimentionsUtil.dp2px(this, 260);
        IApplicationProvider provider = RouterHelper.getProvider(RouterPath.APPLICATION_PROVIDER);
        String myAppCode = AppCode.getMyAppCode(provider.getProjectBuildInfo().application_id);
        Bitmap bitmap = CodeUtils.createImage("https://m.8btc.com/im/qr-friend?source="+myAppCode+"&id="+mFriendID, wh, wh, null);
        mIvQr.setImageBitmap(bitmap);
    }

    @OnClick({R2.id.fl_back, R2.id.fl_save, R2.id.iv_im_circle, R2.id.iv_wechat, R2.id.iv_circle})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.fl_back) {
            finish();
        } else if (i == R.id.fl_save) {
            //保存二维码到手机
            shareQr(MEDIA_TYPE.SAVE);
        } else if (i == R.id.iv_im_circle) {
            //分享到im
            shareCircle();
        } else if (i == R.id.iv_wechat) {
            //分享到微信
            shareQr(MEDIA_TYPE.WEIXIN);
        } else if (i == R.id.iv_circle) {
            //分享到朋友圈
            shareQr(MEDIA_TYPE.WEIXIN_CIRCLE);
        }
    }

    private void shareQr(final MEDIA_TYPE media){
        ShareResTask.doTask(new ShareResTask.NormalBitmapTask(mShareArea) {
            @Override
            public void doOnUIThread(Bitmap bitmap) {
                //缩放至宽固定的大小
                final Bitmap zoomBitmap = ScreenShotUtil.zoomImg(bitmap, 1080);
                if (media == MEDIA_TYPE.SAVE){
                    ScreenShotUtil.savePicture(mContext, zoomBitmap, 100);
                    ToastUtil.show("保存成功");
                }else {
                    SharedUtils.sharePic(PersonalQrCodeActivity.this, media.mMedia, zoomBitmap, null);
                }
            }
        });
    }


    private void shareCircle() {
        ShareContent content = new ShareContent();
        content.id = mFriendID;
        content.mTitle = mName;
        content.mDescribe = mDesc;
        content.mThumb = mAvatar;
        content.mUrl = "blockmeta://im/user?id="+mFriendID;
        content.mTypeDescribe = TypeDescribe.USER_CARD;
        content.mShareType = IShareObject.ShareType.WEB_PAGE;
        ARouter.getInstance().build(RouterPath.IM_SHARE_ACTIVITY)
                .withParcelable(Constant.ZERO,content)
                .navigation(mContext);
    }
}
